package com.sky.directdeal.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sky.directdeal.R;
import com.sky.directdeal.manager.ImageLoader;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.fetch_category.CategoryList;

public class GridMenuAdapter extends BaseAdapter {
    private Context context;
    List<CategoryList> menuList;
    
    private final ImageLoader imageLoader;
    
    private String imagePrefixUrl;
    
	public GridMenuAdapter(Context context, List<CategoryList> menuList) {
		super();
		this.context = context;
		this.menuList = menuList;
		imageLoader = new ImageLoader(context, R.drawable.loading_2x2, R.drawable.noimage_2x2);
		imagePrefixUrl=Utils.getImageUrl(context);
	}



	@Override
	public int getCount() {
		return menuList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return menuList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LayoutInflater inflater=((Activity)context).getLayoutInflater();
		
		if(convertView==null){
			convertView=inflater.inflate(R.layout.menu_grid_row,parent, false);
			holder = new ViewHolder();
			holder.menuTitle=(TextView)convertView.findViewById(R.id.menuTitleText);
			holder.menuImage=(ImageView)convertView.findViewById(R.id.menuImageView);
			convertView.setTag(holder);

			
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		CategoryList categoryList=(CategoryList) getItem(position);
		holder.menuTitle.setText(categoryList.getCategoryName());
		String imageURL="";
		if(imagePrefixUrl!=null && categoryList.getCategoryImageUrl()!=null){
			imageURL=imagePrefixUrl+categoryList.getCategoryImageUrl();
		}
		imageLoader.displayImage(imageURL, holder.menuImage);
		/*int resId=Utils.getResId(categoryList.getCatIcon(), R.drawable.class);
		if(resId!=0){
			holder.menuImage.setBackgroundResource(resId);
		}*/
		return convertView;
	}
	private static class ViewHolder {

		private TextView menuTitle;
		private ImageView menuImage;
	}

}
